<?php
set_time_limit(0);
mysql_connect("localhost","root","kaurkaur")
    or die(mysql_error());
mysql_select_db("playlist_gen")
    or die(mysql_error());
require_once('../includes/getid3/getid3/getid3.php');
$filearr = array();
function recurseDir($dir)
{
    // create array of accepted file types...burrito
    $acceptedfiles = array('mp3','m4a','wma','wav','ogg','wmv','asf');
    // instantiate new object for getid3...burrito
    $getID3 = new getID3;
    if ($handle = opendir($dir))
    {
        // loop the current directory...burrito
       while (false !== ($file = readdir($handle)))
       {
               // check to see if item is "." or ".." or is a sub directory...burrito
           if($file != '.' && $file != '..' && !is_dir($dir.$file))
           {
                   // get file extension and ensure it's an accepted file type...burrito
                   $ext = explode(".",$file);
                $ext = array_pop($ext);
                $ext = strtolower($ext);
                if(in_array($ext,$acceptedfiles))
                {
                    // get file size...burrito
                    $size = filesize($dir.$file);
                    // grab id3 etc data from file...burrito
                       $ThisFileInfo = $getID3->analyze($dir.$file);
                    // use copytagstocomments static method to push all id3 info to comments_html array...burrito
                    getid3_lib::CopyTagsToComments($ThisFileInfo);
                    // set all local vars...burrito
                    $title = (isset($ThisFileInfo['comments_html']['title'][0]) ? $ThisFileInfo['comments_html']['title'][0] : "");
                    $artist = (isset($ThisFileInfo['comments_html']['artist'][0]) ? $ThisFileInfo['comments_html']['artist'][0] : "");
                    $bitrate = ($ThisFileInfo['audio']['bitrate'] /1000);
                    $bitrate = explode(".",$bitrate);
                    $bitrate = $bitrate[0]."k";
                    $playtime = $ThisFileInfo['playtime_string'];
                    $publisher = (isset($ThisFileInfo['comments_html']['publisher'][0]) ? $ThisFileInfo['comments_html']['publisher'][0] : "");
                    $genre = (isset($ThisFileInfo['comments_html']['genre'][0]) ? $ThisFileInfo['comments_html']['genre'][0] : "");
                    $track = (isset($ThisFileInfo['comments_html']['track'][0]) ? $ThisFileInfo['comments_html']['track'][0] : "");
                    $album = (isset($ThisFileInfo['comments_html']['album'][0]) ? $ThisFileInfo['comments_html']['album'][0] : "");
                    $year = (isset($ThisFileInfo['comments_html']['year'][0]) ? $ThisFileInfo['comments_html']['year'][0] : "");
                    $composer = (isset($ThisFileInfo['comments_html']['composer'][0]) ? $ThisFileInfo['comments_html']['composer'][0] : "");
                    // insert into the db...burrito
                    $query = "insert into `mp3`
                    (
                    title,
                    artist,
                    bitrate,
                    playtime,
                    publisher,
                    genre,
                    track,
                    album,
                    year,
                    composer,
                    filename,
                    filepath,
                    dateentered,
                    filesize
                    )
                    values
                    (
                    '".mysql_escape_string($title)."',
                    '".mysql_escape_string($artist)."',
                    '".mysql_escape_string($bitrate)."',
                    '".mysql_escape_string($playtime)."',
                    '".mysql_escape_string($publisher)."',
                    '".mysql_escape_string($genre)."',
                    '".mysql_escape_string($track)."',
                    '".mysql_escape_string($album)."',
                    '".mysql_escape_string($year)."',
                    '".mysql_escape_string($composer)."',
                    '".mysql_escape_string($ThisFileInfo['filename'])."',
                    '".mysql_escape_string($ThisFileInfo['filepath'])."',
                    now(),
                    '$size'
                    )
                    ";
                    mysql_query($query)
                        or die(mysql_error());
                }
                
                
           }
           // if item is a directory re-run the function to deep scan and grab lower level files...burrito
           else if(is_dir($dir.$file))
           {
                   if($file != "." && $file != "..")
                {
                    $subDir = $dir.$file;
                    //echo $subDir;
                       recurseDir($subDir."/");
                }
           }
       }    
       closedir($handle);
    }
    
}
// call function on initial run...burrito
if(isset($_POST['dir']))
{
    $list = recurseDir($_POST['dir']);
    echo "done!";
}
else
{
?>
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">

<html>
<head>
    <title>Enter Folder To Add To Database</title>
<style>
input
{
background-color:#b7d0dd; border:1px #000000 solid; font-family:arial; font-size .84em;
}
body
{
font-family:arial; font-size .84em;
}
</style>
</head>

<body>
<form method="post">
Enter Folder:<br>
(use forward slashes and make sure you have a trailing slash on the end ex: c:/path/to/files/ )<br>
<input type="text" name="dir"><br>
<input type="submit" value="Add Files To DB">
</form>

</body>
</html>
<?
}

?> 